package main

import (
	"fmt"
	"strconv"
)

func int2Float() {
	var x = 12
	var y = 12.1233
	//fmt.Println(y + x) //错误的写法
	fmt.Println(y + float64(x))
}

func float2Int() {
	var x = 12
	var y = 12.1233
	fmt.Println(int(y) + x)
}

func rune2String() {
	//rune is an alias for int32;
	//rune == int32
	var x rune = 'a'
	var y int32 = 'b'
	fmt.Println(x)
	fmt.Println(y)
	fmt.Println(string(x))
	fmt.Println(string(y))
}

func bytes2String() {
	var bts = []byte{'h', 'e', 'l', 'l', 'o'}
	fmt.Println(bts)
	word := string(bts)
	fmt.Println(word)
}

func string2bytes() {
	word := "hello"
	var bts = []byte(word)
	fmt.Println(bts)
}

//strconv -> string -> int(10进制)
func stringConvAtoi() {
	var x = "12"
	var y = 6
	z, _ := strconv.Atoi(x)
	fmt.Println("z = ", z)
	fmt.Println("y = ", y)
	fmt.Println("y + z = ", y+z)
}

func stringConvItoa() {
	x := 12
	y := "I have this many: " + strconv.Itoa(x)
	//z := x +y  //编译不过 类型不同不能加
	fmt.Println(y)
}

//int8:   -128 ~ 127
//int16:  -32768 ~ 32767
//int32:  -2147483648 ~ 2147483647
//int64:  -9223372036854775808 ~ 9223372036854775807
//
//uint8:  0 ~ 255
//uint16: 0 ~ 65535
//uint32: 0 ~ 4294967295
//uint64: 0 ~ 18446744073709551615
func stringConvParseOthers() {
	b, _ := strconv.ParseBool("true")
	fmt.Printf("%T  %v\n", b, b)

	f, _ := strconv.ParseFloat("3.1415", 64)
	fmt.Printf("%T  %v\n", f, f)

	i, _ := strconv.ParseInt("-42", 10, 64)
	fmt.Printf("%T  %v\n", i, i)

	ui, _ := strconv.ParseUint("42", 10, 64)
	fmt.Printf("%T  %v\n", ui, ui)

	ui2, err := strconv.ParseUint("-42", 10, 64)
	//err = strconv.ParseUint: parsing "-42": invalid syntax
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Printf("%T  %v\n", ui2, ui2)
	}

}

func main() {
	int2Float()
	fmt.Println("=============================")
	float2Int()
	fmt.Println("=============================")
	rune2String()
	fmt.Println("=============================")
	bytes2String()
	fmt.Println("=============================")
	string2bytes()
	fmt.Println("=============================")
	stringConvAtoi()
	fmt.Println("=============================")
	stringConvItoa()
	fmt.Println("=============================")
	stringConvParseOthers()
}
